package com.woniuxy.movice.service.imp;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.woniuxy.movice.config.AliPayConfig;
import com.woniuxy.movice.mapper.PayMapper;
import com.woniuxy.movice.service.PayService;

@Service
@Transactional
public class PayServiceImp implements PayService{

	@Override
	public String pay(String out_trade_no, String subject, String total_amount, String body) throws AlipayApiException {
		  // 1、获得初始化的AlipayClient
        String serverUrl = AliPayConfig.gatewayUrl;
        String appId = AliPayConfig.app_id;
        String privateKey = AliPayConfig.merchant_private_key;
        String format = "json";
        String charset = AliPayConfig.charset;
        String alipayPublicKey = AliPayConfig.alipay_public_key;
        String signType = AliPayConfig.sign_type;
        String returnUrl = AliPayConfig.return_url;
        String notifyUrl = AliPayConfig.notify_url;
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey, format, charset, alipayPublicKey, signType);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(returnUrl);
        alipayRequest.setNotifyUrl(notifyUrl);

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"10m\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //请求
        String result = alipayClient.pageExecute(alipayRequest).getBody();
        // 返回付款信息
       return result;
	}

}
